<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" :model="userInfo" :rules="rules" label-width="220px">
          <!-- 姓名-->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="userInfo.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 -->
          <el-row>
            <el-col :span="12">
              <el-form-item prop="workNumber" label="工号">
                <el-input v-model="userInfo.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="userInfo.mobile"
                  :disabled="!!$route.params.id"
                  size="mini"
                  class="inputW"
                />
                <!--
                   <el-input
                  v-model="userInfo.mobile"
                  :disabled="$route.params.id"
                  size="mini"
                  class="inputW"
                /> -->
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item v-model="userInfo.departmentId" label="部门">
                <!-- TODO 放置及联部门组件 -->
                <!-- 父级使用v-model 给子级绑定部门ID  -->
                <select-tree v-model="userInfo.departmentId" class="inputW" />

              </el-form-item>
            </el-col>
          </el-row>
          <!-- 聘用形式 -->
          <el-row>
            <el-col :span="12">
              <!-- label:给用户看 -->
              <!-- value:给程序看 -->
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="userInfo.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="userInfo.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 转正时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="userInfo.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片组件 -->
                <image-upload v-model="userInfo.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="saveData">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import selectTree from './components/select-tree.vue'
import { addEmployeeAPI, getEmployeeaDetailAPI, updateEmployeeAPI } from '@/api/employee'
import ImageUpload from './components/image-upload'
export default {
  components: {
    selectTree,
    ImageUpload

  },
  data() {
    return {
      userInfo: {
        username: '', // 用户名
        mobile: '', // 手机号
        workNumber: '', // 工号
        formOfEmployment: null, // 聘用形式
        departmentId: null, // 部门id
        timeOfEntry: '', // 入职时间
        correctionTime: '', // 转正时间
        staffPhoto: '' // 用户头像
      },
      rules: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' }, {
          min: 1, max: 4, message: '姓名为1-4位'
        }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }, {
        //   pattern 正则表达式
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '请选择聘用形式', trigger: 'blur' }],
        departmentId: [{ required: true, message: '请选择部门', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请选择入职时间', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '请选择转正时间', trigger: 'blur' }, {
          validator: (rule, value, callback) => {
            if (this.userInfo.timeOfEntry) {
              if (new Date(this.userInfo.timeOfEntry) > new Date(value)) {
                callback(new Error('转正时间不能小于入职时间'))
                return
              }
            }
            callback()
          }
        }]
      }

    }
  },
  // 在页面一打开 created()就被调用
  created() {
    // 获取<查看>跳转时传入的员工ID(路径参数)
    // console.log(this.$route.params.id)
    // 获取当前员工详情数据并回显到表单
    if (this.$route.params.id) {
      this.getEmployeeDetail()
    }
  },
  methods: {
    // 获取当前员工详情数据并回显到表单
    async getEmployeeDetail() {
      // 调用员工详情数据接口方法
      this.userInfo = await getEmployeeaDetailAPI(this.$route.params.id)
    },

    // 点击保存更新的按钮
    // this.$refs.userForm.validate 表示 <el-form></el-form> 下的表单整体校验
    saveData() {
      this.$refs.userForm.validate(async(isOk) => {
        if (isOk) {
          // 判断当前是新增员工还是编辑员工
          if (this.$route.params.id) {
            // 编辑员工
            // 校验通过，调用编辑员工的接口方法
            await updateEmployeeAPI(this.userInfo)
            // 编辑员工成功，友好提示: 编辑员工成功
            this.$message.success('编辑员工成功')
          } else {
            // 新增员工
            // 校验通过，调用新增员工的接口方法
            await addEmployeeAPI(this.userInfo)
            // 新增员工成功，友好提示: 新增员工成功
            this.$message.success('新增员工成功')
          }

          // // 调回到员工列表页即可
          this.$router.push('/employee')
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
    .edit-form {
      background: #fff;
      padding: 20px;
      .inputW {
        width: 380px
      }
    }

</style>

